package camuyen.ltwlibrary.dao;

import java.util.ArrayList;
import java.util.List;

import camuyen.ltwlibrary.model.Author;
import org.hibernate.Query;
import org.hibernate.Session;

import camuyen.ltwlibrary.model.Category;
import camuyen.ltwlibrary.model.User;
import camuyen.ltwlibrary.util.HibernateUtil;

public class CategoryDao {
	public static Category getCategory(Long categoryId) {
        Session session = HibernateUtil.openSession();
        Category category = (Category) session.load(Category.class, categoryId);
        session.close();
        return category;
    }

    public static void saveCategory(Category category) {
        Session session = HibernateUtil.openSession();
        session.getTransaction().begin();
        session.save(category);
        session.getTransaction().commit();
        session.close();
    }
    
    public static void updateCategory(Category category) {
        Session session = HibernateUtil.openSession();
        session.getTransaction().begin();
        session.update(category);
        session.getTransaction().commit();
        session.close();
    }
    
    public static List<Category> getAll() {
    	Session session = HibernateUtil.openSession();
    	Query query = session.createQuery("SELECT cat FROM Category cat");
    	List<Category> allCategory = (List<Category>) query.list();
        session.close();
    	return allCategory;
    }

    public static List<Category> getCategoriesByListId(List<Long> listId) {
        List<Category> listCategory = new ArrayList<Category>();
        for (Long id: listId) {
            listCategory.add(CategoryDao.getCategory(id));
        }

        return listCategory;
    }
}
